//-----------------------------------------------------------------------
// <copyright file="a_SexDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_SexDAOBase
    {
        #region Common methods
        public virtual a_Sex Createa_SexFromReader(IDataReader reader)
        {
            a_Sex item = new a_Sex();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SexID"))) item.SexID = (int)reader["SexID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SexNameVN"))) item.SexNameVN = (string)reader["SexNameVN"];
                if (!reader.IsDBNull(reader.GetOrdinal("SexNameEN"))) item.SexNameEN = (string)reader["SexNameEN"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Sex methods
            
        public virtual void Createa_Sex(a_Sex a_Sex)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sexs_ins");
                
                database.AddInParameter(dbCommand, "@SexNameVN", DbType.String, a_Sex.SexNameVN);
                database.AddInParameter(dbCommand, "@SexNameEN", DbType.AnsiString, a_Sex.SexNameEN);
                database.AddOutParameter(dbCommand, "@SexID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Sex.SexID = (int)database.GetParameterValue(dbCommand, "@SexID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Sex methods
        
        public virtual void Updatea_Sex(a_Sex a_Sex)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sexs_upd");            
                
                database.AddInParameter(dbCommand, "@SexID", DbType.Int32, a_Sex.SexID);
                database.AddInParameter(dbCommand, "@SexNameVN", DbType.String, a_Sex.SexNameVN);
                database.AddInParameter(dbCommand, "@SexNameEN", DbType.AnsiString, a_Sex.SexNameEN);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Sex methods
        public virtual void Deletea_Sex(int sexID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sexs_del");
                
                database.AddInParameter(dbCommand, "@SexID", DbType.Int32, sexID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Sex methods
        
        public virtual a_Sex Geta_Sex(int sexID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sexs_sel");
                
                database.AddInParameter(dbCommand, "@SexID", DbType.Int32, sexID);
                
                a_Sex a_Sex =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Sex = Createa_SexFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Sex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_SexList methods
        public virtual a_SexCollection Geta_SexList(a_SexColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sexs_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_SexCollection a_SexCollection = new a_SexCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Sex a_Sex = Createa_SexFromReader(reader);
                        a_SexCollection.Add(a_Sex);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SexCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_SexCollection Geta_SexList(a_SexColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_SexList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}